<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: action.topics.php Sat Jul 18 13:22:15 CST 2009
 */

include "header.php";

$op = isset($_POST["op"]) ? trim($_POST["op"]) : "";
$ac = isset($_POST["ac"]) ? trim($_POST["ac"]) : "";
$groups_handler = xoops_getmodulehandler("groups","groups");
$threads_handler = xoops_getmodulehandler("threads","groups");
$posts_handler = xoops_getmodulehandler("posts","groups");
$myts =& MyTextSanitizer::getInstance();

$group_id = isset($_REQUEST["group_id"]) ? intval($_REQUEST["group_id"]) : 0;

if ( empty($group_id) ) redirect_header("topics.php");
$group_obj = $groups_handler->get($group_id);

if ( !is_object($group_obj) ) redirect_header("topics.php");

switch ($op) {
	case "mvtopics":
		switch ($ac) {
			default:
				$_tids = $_POST["tids"];
				$group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0;
				if ( count($_tids) ) {
					$tids = serialize($_tids);
				}
				xoops_confirm(array('group_id' => $group_id,"tids"=>$tids, 'ac' => "save","op"=>"mvtopics"), 'action.topics.php', "确定将这些主题放入到 [".$group_obj->getVar("group_name")."] 圈子下?");
			break;
			
			case "save":
				if (!$GLOBALS['xoopsSecurity']->check()) {
		            redirect_header("topics.php", 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
		            exit();
		        }
				$_tids = $_POST["tids"];
				$tids = unserialize($myts->stripSlashesGPC($_tids));
				
				$group_id = isset($_POST["group_id"]) ? intval($_POST["group_id"]) : 0;
				$group_obj = $groups_handler->get($group_id);
				$stop = "";
				if ( count($tids) && !empty($group_id) ) {
					$criteria = new CriteriaCompo(new Criteria("tid",implodeArrayforSql($tids),"in"));
					if ( $threads_handler->updateAll("group_id",$group_id,$criteria) ) {
						
						$group_obj->setVar("group_topics",$group_obj->getVar("group_topics") + count($tids) );
						$groups_handler->insert($group_obj);
						$stop .= "主题操作成功";
					} else {
						$stop .= "主题操作失败";
					}
					if ( $posts_handler->updateAll("group_id",$group_id,$criteria) ) {
						$post_counts = $posts_handler->getCount($criteria);
						$group_obj->setVar("group_posts",$group_obj->getVar("group_posts") + $post_counts - 1 );
						$groups_handler->insert($group_obj);
						$stop .= ", 回复操作成功";
					} else {
						$stop .= ", 回复操作失败";
					}
				} else {
					$stop = "没有选择主题";
				}
				
				redirect_header("topics.php",5,$stop);
			break;
		}
	break;
}
include "footer.php";
?>